import 'package:couplets_front/config/config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:dio/dio.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '楹联丛话',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List couplets = [];

  Future _getdata() async {
    Dio dio = Dio();
    print("===========================");
    await dio.get("http://" + ipaddr + "/items").then((Response res) {
      if (res.statusCode == 200) {
        couplets.clear();
        for (var value in res.data['result']) {
          couplets.add(value);
          print(value);
        }
      }
    }).catchError((e) => print(e.toString()));
    //return couplets;
  }

  _onclick() async {
    _getdata();
    setState(() {});
  }

  Widget _getWords(String text) {
    // const text =
    //     "曾经沧海难为水,除却巫山不是云;取次花丛懒回顾,半缘修道半缘君哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈啊哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈";
    List<Widget> left = [];
    var words = text.split("");
    List<Widget> right = [];
    for (var word in words) {
      if (word == ";") {
        left = right;
        right = [];
        continue;
      }
      right.add(RotatedBox(
          quarterTurns: 4,
          child: Text(
            word,
            style: TextStyle(fontSize: 22,color: Colors.white),
          )));
      right.add(SizedBox(
        height: 4,
      ));
    }
    return Container(
        height: 330,
        padding: EdgeInsets.all(20),
        child: ListView(
          scrollDirection: Axis.horizontal,
          reverse: true,
          children: [
            Row(
              textDirection: TextDirection.rtl,
              children: [
                Wrap(
                  textDirection: TextDirection.rtl,
                  direction: Axis.vertical,
                  children: left,
                ),
                SizedBox(
                  width: 22,
                ),
                Wrap(
                  textDirection: TextDirection.rtl,
                  direction: Axis.vertical,
                  children: right,
                )
              ],
            ),
          ],
        ));
  }

  Widget _buildItem(BuildContext context, int index) {
    return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(20)),
        child: Stack(
          fit: StackFit.expand,
          children: [
            Image.asset(
              images[index % images.length],
              fit: BoxFit.fill,
            ),
            Container(
              padding: EdgeInsets.all(8.0),
              child: Column(
                textDirection: TextDirection.rtl,
                children: [_getWords(couplets[index % couplets.length])],
              ),
            ),
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          gradient: LinearGradient(
              colors: [
            Color(0xFF1b1e44),
            Color(0xFF2d3447),
          ],
              begin: Alignment.bottomCenter,
              end: Alignment.topCenter,
              tileMode: TileMode.clamp)),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: SingleChildScrollView(
          child: Column(
            children: [
              Padding(
                padding:
                    EdgeInsets.only(left: 12, right: 12, top: 30, bottom: 8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    IconButton(
                        icon: Icon(
                          CustomIcons.menu,
                          color: Colors.white,
                          size: 30,
                        ),
                        tooltip: "test",
                        onPressed: () {}),
                    IconButton(
                      icon: Icon(
                        Icons.search,
                        color: Colors.white,
                        size: 30,
                      ),
                      onPressed: () {
                        print(couplets);
                      },
                    )
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "楹联大观",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 46,
                          letterSpacing: 1.0),
                    ),
                    IconButton(
                      icon: Icon(
                        CustomIcons.option,
                        size: 12,
                        color: Colors.white,
                      ),
                      onPressed: () {},
                    )
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 20),
                child: Row(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                          color: Colors.deepOrange,
                          borderRadius: BorderRadius.circular(20)),
                      height: 30,
                      width: 80,
                      child: Center(
                        // child: Padding(
                        //   padding:
                        //       EdgeInsets.symmetric(horizontal: 22, vertical: 5),
                        //   child: Text(
                        //     '更新',
                        //     style: TextStyle(color: Colors.white),
                        //   ),
                        // ),
                        child: FlatButton(
                            onPressed: _onclick,
                            child: Text(
                              '更新',
                              style: TextStyle(color: Colors.white),
                            )),
                      ),
                    ),
                    SizedBox(
                      width: 25,
                    ),
                    Text(
                      '+more',
                      style: TextStyle(color: Colors.blueAccent),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 20,
              ),
              FutureBuilder(
                future: _getdata(),
                builder: (context, snapshort) {
                  var widget;
                  if (snapshort.connectionState == ConnectionState.done) {
                    if (snapshort.hasError) {
                      widget = Icon(
                        Icons.error,
                        color: Colors.red,
                        size: 48,
                      );
                    } else {
                      widget = Swiper(
                        itemBuilder: _buildItem,
                        itemCount: 10,
                        itemHeight: 350,
                        itemWidth: 300,
                        layout: SwiperLayout.STACK,
                        customLayoutOption:
                            CustomLayoutOption(startIndex: 0, stateCount: 3)
                                .addRotate(
                                    [-25 / 180, 0.0, 25.0 / 180]).addTranslate([
                          new Offset(-450.0, 0.0),
                          new Offset(0.0, 0.0),
                          new Offset(450.0, 0.0)
                        ]),
                      );
                    }
                  } else {
                    couplets.add("加载中");
                    widget = Swiper(
                      itemBuilder: _buildItem,
                      itemCount: 10,
                      itemHeight: 350,
                      itemWidth: 300,
                      layout: SwiperLayout.STACK,
                      customLayoutOption:
                          CustomLayoutOption(startIndex: 0, stateCount: 3)
                              .addRotate(
                                  [-25 / 180, 0.0, 25.0 / 180]).addTranslate([
                        new Offset(-450.0, 0.0),
                        new Offset(0.0, 0.0),
                        new Offset(450.0, 0.0)
                      ]),
                    );
                  }
                  return Container(child: widget);
                },
              ),
              SizedBox(
                height: 30,
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'Favourite',
                      style: TextStyle(
                          color: Colors.white, fontSize: 46, letterSpacing: 1),
                    ),
                    IconButton(
                        icon: Icon(
                          CustomIcons.option,
                          size: 12,
                          color: Colors.white,
                        ),
                        onPressed: () {}),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 20),
                child: Row(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                          color: Colors.blueAccent,
                          borderRadius: BorderRadius.circular(20)),
                      child: Center(
                        child: Padding(
                          padding:
                              EdgeInsets.symmetric(horizontal: 22, vertical: 6),
                          child: Text(
                            'data',
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 15,
                    ),
                    Text(
                      '9+stories',
                      style: TextStyle(color: Colors.blueAccent),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 20,
              ),
              FutureBuilder(
                future: _getdata(),
                builder: (context, snapshort) {
                  var widget;
                  if (snapshort.connectionState == ConnectionState.done) {
                    if (snapshort.hasError) {
                      widget = Icon(
                        Icons.error,
                        color: Colors.red,
                        size: 48,
                      );
                    } else {
                      widget = Swiper(
                        itemBuilder: _buildItem,
                        itemCount: 3,
                        itemHeight: 250,
                        itemWidth: 300,
                        layout: SwiperLayout.CUSTOM,
                        customLayoutOption:
                            CustomLayoutOption(startIndex: 0, stateCount: 3)
                                .addRotate(
                                    [-25 / 180, 0.0, 25.0 / 180]).addTranslate([
                          new Offset(-450.0, 0.0),
                          new Offset(0.0, 0.0),
                          new Offset(450.0, 0.0)
                        ]),
                      );
                    }
                  } else {
                    couplets.add("加载中");
                    widget = Swiper(
                      itemBuilder: _buildItem,
                      itemCount: 3,
                      itemHeight: 250,
                      itemWidth: 300,
                      layout: SwiperLayout.CUSTOM,
                      customLayoutOption:
                          CustomLayoutOption(startIndex: 0, stateCount: 3)
                              .addRotate(
                                  [-25 / 180, 0.0, 25.0 / 180]).addTranslate([
                        new Offset(-450.0, 0.0),
                        new Offset(0.0, 0.0),
                        new Offset(450.0, 0.0)
                      ]),
                    );
                  }
                  return Container(child: widget);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class CustomIcons {
  static const IconData menu = IconData(0xe900, fontFamily: "CustomIcons");
  static const IconData option = IconData(0xe902, fontFamily: "CustomIcons");
}

const List<String> images = [
  "assets/image_01.png",
  "assets/image_02.jpg",
  "assets/image_03.jpg",
  "assets/image_04.jpg",
];

const List<String> titles = [
  "Flutter Swiper is awosome",
  "Really nice",
  "Yeap"
];
